package com.dqhl.community.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.dqhl.community.R;
import com.dqhl.community.adapter.OrderSingleListAdapter;
import com.dqhl.community.base.BaseActivity;
import com.dqhl.community.model.OrderRecord;
import com.dqhl.community.util.Constant;
import com.dqhl.community.util.Dlog;

import java.util.List;

/**
 * Created by mac on 16/11/18.
 * 单个订单activity
 */
public class OrderSingleActivity extends BaseActivity implements View.OnClickListener {

    private Context context;
    private ImageView iv_top_back;
    private TextView tv_top_title;
    private ListView lv_order;
    private OrderRecord orderRecord;
    private List<OrderRecord.Good> goodList;
    private OrderSingleListAdapter orderSingleListAdapter;
    private TextView tv_order_id;
    private TextView tv_status;
    private TextView tv_total_number;
    private TextView tv_total_money;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_single);
        context = this;

        registerUpdateReceiver();
        initView();
        initData();
    }

    BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(Constant.ACTION_EVALUATE_SUCCESS)) {
                initData();
            }
        }
    };

    private void registerUpdateReceiver() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Constant.ACTION_EVALUATE_SUCCESS);
        registerReceiver(receiver, intentFilter);
    }

    private void initView() {
        iv_top_back = (ImageView) findViewById(R.id.iv_top_back);
        iv_top_back.setVisibility(View.VISIBLE);
        tv_top_title = (TextView) findViewById(R.id.tv_top_title);
        tv_top_title.setText("订单");
        lv_order = (ListView) findViewById(R.id.lv_order);
        tv_order_id = (TextView) findViewById(R.id.tv_order_id);
        tv_status = (TextView) findViewById(R.id.tv_status);
        tv_total_number = (TextView) findViewById(R.id.tv_total_number);
        tv_total_money = (TextView) findViewById(R.id.tv_total_money);

        iv_top_back.setOnClickListener(this);
    }

    private void initData() {
        orderRecord = (OrderRecord) getIntent().getSerializableExtra("order");
        Dlog.e(orderRecord.toString());
        goodList = orderRecord.getGoods();
        String orderId = orderRecord.getName();
        String status = orderRecord.getStatus();
        String total_number = orderRecord.getNumber() + "";
        String total_money = orderRecord.getTotal();

        //显示订单状态
        switch (status) {
            case "1":
                tv_status.setText("订单已生成");
                break;
            case "2":
                tv_status.setText("已取消");
                break;
            case "3":
                tv_status.setText("付款成功");
                break;
            case "4":
                tv_status.setText("货到付款");
                break;
            case "5":
                tv_status.setText("已发货");
                break;
            case "6":
                tv_status.setText("配送中");
                break;
            case "7":
                tv_status.setText("货到付款已支付");
                break;
            case "8":
                tv_status.setText("买家签收");
                break;
            case "9":
                tv_status.setText("他人代收");
                break;
            case "10":
                tv_status.setText("签收异常");
                break;
            case "11":
                tv_status.setText("退货");
                break;
            case "12":
                tv_status.setText("退款");
                break;
            case "13":
                tv_status.setText("订单完成");
                break;
        }
        tv_order_id.setText(orderId);
        tv_total_number.setText(total_number);
        tv_total_money.setText(total_money);
        orderSingleListAdapter = new OrderSingleListAdapter(context, goodList, status,
                new OrderSingleListAdapter.OnTextClickListener() {
                    @Override
                    public void onEvaluate(int position) {
                        Bundle bundle = new Bundle();
                        bundle.putString("order_id", orderRecord.getId());
                        bundle.putString("order_name", orderRecord.getName());
                        bundle.putSerializable("goods", goodList.get(position));
                        overlay(EvaluateActivity.class, bundle);
                    }
                });
        lv_order.setAdapter(orderSingleListAdapter);
//        lv_order.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                String order_id = orderRecord.getId();
//                Dlog.e("clicked_order_id:" + order_id);
//                Bundle bundle = new Bundle();
//                bundle.putString("order_id", order_id);
//                overlay(DistributionDetailActivity.class, bundle);
//            }
//        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_top_back:
                finish();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (receiver != null) {
            unregisterReceiver(receiver);
        }
    }
}
